{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">

            <div class="layui-form">
                <div class="layui-btn-group ">

                </div>
                <div class="layui-input-inline">
                    <select name="category_id" lay-verify="required" id="category_id">
                        <option value="">请选择分类</option>

                        {foreach $data as $v}
                        <option value="{$v.id}">{$v.name}</option>
                        {if isset($v.childs) && !empty($v.childs)}
                        {foreach $v.childs as $vv}
                        <option value="{$vv.id}">&nbsp;&nbsp;&nbsp;┗━━{$vv.name}</option>
                        {if isset($vv.childs) && !empty($vv.childs)}
                        {foreach $vv.childs as $vvv}
                        <option value="{$vvv.id}">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;┗━━{$vvv.name}</option>
                        {/foreach}
                        {/if}
                        {/foreach}
                        {/if]
                        {/foreach}
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="title" id="title" placeholder="请输入课程标题" class="layui-input">
                </div>
                <button class="layui-btn" id="searchBtn"><i class="layui-icon">&#xe615;</i>搜 索</button>
                <a class="layui-btn" href="{:url('mall.account/export')}"><i class="layui-icon">&#xe615;</i>导出</a>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm {:auth('mall.account/edit')}" lay-event="edit">预览详情</a>
                </div>
            </script>
            <script type="text/html" id="thumb">
                {{#  if(d.thumb){ }}
                <a href="{{d.thumb}}" target="_blank" title="点击查看">
                    <img src="{{d.thumb}}" alt="" width="28" height="28">
                </a>
                {{#  } }}
            </script>
            <!-- 表格状态列 -->
            <script type="text/html" id="tableStateUser">
                <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
                       lay-text="通过|驳回" {{d.status==1?'checked':''}}/>
            </script>
            <script type="text/html" id="category">
                {{# if(d.category){ }}
                {{d.category.name}}
                {{# }else { }}
                分类不存在
                {{#  } }}
            </script>
            <script type="text/html" id="store">
                {{# if(d.store){ }}
                {{d.store.name}}
                {{# }else { }}
                商家不存在
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'table', 'admin','form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        //用户表格初始化
        var dataTable =
                table.render({
                    elem: '#dataTable'
                    , autoSort: false
                    , height: 500
                    , url: '{:__url("mall.account/getData")}' //数据接口
                    , page: true //开启分页
                    ,sort :true
                    ,toolbar: true
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'category', title: '分类', toolbar: '#category'}
                        , {field: 'store', title: '所属商家', toolbar: '#store'}
                        , {field: 'title', title: '标题'}
                        , {field: 'sale', sort:true, title: '销量'}
                        , {field: 'thumb', title: '主图', toolbar: '#thumb', width: 100}

                        ,{
                            title: '状态', templet: function (d) {
                                var strs = ['<span class="layui-badge layui-bg-blue">待审核</span>','<span class="layui-badge layui-bg-green">已通过</span>', '<span class="layui-badge layui-bg-red">已驳回</span>'];
                                return strs[d.status];
                            }, align: 'center'
                        }
                        , {field: 'click', title: '点击量'}

                        , {field: 'update_time', title: '更新时间'}

//                        ,{field: 'status', sort: true, templet: '#tableStateUser', title: '审核操作'}
                        , {fixed: 'right', width: 140, align: 'center', title:'预览详情', toolbar: '#options'}
                    ]]
                });

        //监听工具条
        table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post('{:__url("mall.account/action")}',{
                        _method: 'delete',
                        ids: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                showEditModel('预览'+data.title,'/admin/mall.account/edit?id='+ data.id);

            }
        });


        // 显示表单弹窗
        function showEditModel(title,url){
            admin.open({
                type: 2,
                offset:'r',
                title: title,
                content: url,
                area: ['500px', '100%'],
                btn:['通过','驳回'],
                btnAlign: 'l',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },

                yes: function (index, layero) {

                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {status:1};
                    var action = '{:__url("mall.account/action")}';
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({
                                page: {curr: 1}
                            })

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');

                    return false;

                },
                btn2: function (index, layero) {
                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {status:2};
                    var action = '{:__url("mall.account/action")}';
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({
                                page: {curr: 1}
                            })

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                    return false;
                }


            });
        }

        //审核
        //
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.post('{:__url("mall.account/action")}', {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1,time: 1000},function(index){
//                        window.location.reload();
                        dataTable.reload({
                            page: {curr: 1}
                        })

                    });


                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        //搜索
        $("#searchBtn").click(function () {
            var catId = $("#category_id").val();
            var title = $("#title").val();
            dataTable.reload({
                where: {category_id: catId, title: title},
                page: {curr: 1}
            })
        })
    })
</script>

</body>
</html>
